
//引入本地json数据，这里引入的就是第一步定义的json数据
var jsonData = require('../../data/mock.js');

Page({
    data: {
        category: [
            {name:'小桃气',id:'xiaotaoqi'},
            {name:'莓果恋人',id:'meiguolianren'},
            {name:'鲜果鲜茶',id:'xianguoxiancha'},
            {name:'醇香奶茶',id:'chunxiangnaicha'},
            {name:'益菌多多',id:'yijunduoduo'},
            {name:'法式奶霜',id:'fashinaishuang'},
            {name:'牧场牛奶',id:"muchangniunai"}
        ],
        orderCount: {
          num: 0,
          money: 0
        },
        detail:[],
        taste: [],
        curIndex: 0,
        isScroll: false,
        toView: 'xiaotaoqi',
        bottomFlag: false,
        tasteFlag: false,
        shadeShows: false
    },
    onLoad: function () {
      this.setData({
        //jsonData.dataList获取json.js里定义的json数据，并赋值给dataList
        detail: jsonData.detail_List,
        taste: jsonData.taste_List[0]
      });
    },
  // 下拉刷新
  onPullDownRefresh: function () {
    setTimeout(() => {
      wx.showToast({
        title: '成功加载数据',
        icon: 'success',
        duration: 500
      });
      wx.stopPullDownRefresh()
    }, 500);
  },

    // onReady(){
    //     var self = this;
    //     wx.request({
    //         url:'http://www.gdfengshuo.com/api/wx/cate-detail.txt',
    //         success(res){
    //             self.setData({
    //               detail: jsonData.detail_List
    //             })
    //         }
    //     });
        
    // },
    switchTab(e){
      const self = this;
      this.setData({
        isScroll: true
      })
      setTimeout(function(){
        self.setData({
          toView: e.target.dataset.id,
          curIndex: e.target.dataset.index
        })
      },0)
      setTimeout(function () {
        self.setData({
          isScroll: false
        })
      },1)
        
    },
    // 点击遮罩层，显示的遮罩层与面板又隐藏起来
    shadeClose: function () {
      this.setData({
        tasteFlag: false,
        shadeShows: false
      })
    },
  /* 点击添加商品按钮*/
  addOrder(e) {
    var self = this;
    self.setData({
      tasteFlag: true,
      shadeShows: true,
      detail: this.data.detail,
      taste: this.data.taste
    });
  },
  //加料单选
  getradio: function (e) {
    var jialiao = this.data.taste.jialiao;
    let index = e.currentTarget.dataset.id;
    let radio = this.data.taste.jialiao;
    console.log(this.data.taste.jialiao[0].checked)
    for (let i = 0; i < radio.length; i++) {
      radio[i].checked = false;
    }
    if (radio[index].checked) {
      radio[index].checked = false;
    } else {
     radio[index].checked = true;
    }

    this.setData({
      jialiao:radio
    });
  },
  getradiotemp: function (e) {
    let index = e.currentTarget.dataset.id;
    let radio = this.data.taste.temp;
    console.log(this.data.taste.temp[0].checked)
    for (let i = 0; i < radio.length; i++) {
      radio[i].checked = false;
    }
    if (radio[index].checked) {
      radio[index].checked = false;
    } else {
      radio[index].checked = true;
    }

  },
  getradiosweet: function (e) {
    let index = e.currentTarget.dataset.id;
    let radio = this.data.taste.sweet;
    console.log(this.data.taste.sweet[0].checked)
    for (let i = 0; i < radio.length; i++) {
      radio[i].checked = false;
    }
    if (radio[index].checked) {
      radio[index].checked = false;
    } else {
      radio[index].checked = true;
    }
  },
   /*选取商品点击加入购物车*/
    add_cart:function(){
      this.setData({
        tasteFlag: false,
        shadeShows: false,
        bottomFlag: true
      })
      // let money = 0;
      // let num = subOrders.length;
      // subOrders.forEach(item => {
      //   money += item.price; // 总价格求和
      // });
      // let orderCount = {
      //   num,
      //   money
      // }
      // // 设置显示对应的总数和全部价钱
      // this.setData({
      //   orderCount
      // });
    },

  /**
 * 跳转购物车页面
 */
  toCartPage: function () {
    wx.switchTab({
      url: "/page/component/cart/cart",
    });
  }
    
})